import urllib2
import time

def download_nasdaq(letter):
	csv_url = 'http://www.nasdaq.com/screening/companies-by-name.aspx?letter={0}&render=download'.format(letter)
	headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36'}
	csv_file_request = urllib2.Request(csv_url, headers=headers)
	csv_file_connection = urllib2.urlopen(csv_file_request)
		
	csv_file_path = 'ndq_{0}.csv'.format(letter)
	csv_file = open(csv_file_path, 'wb')
	csv_file.write(csv_file_connection.read())
	csv_file.flush()
	csv_file.close()

letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
indexes = range(len(letters))
for i in indexes:
	print('Downloading listings for: ' + letters[i] + '...')
	download_nasdaq(letters[i])
	time.sleep(5)
	print('Listing for ' + letters[i] + ' letter is downloaded.')
	